package vip.idiy.anwei_android_iot;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import vip.idiy.anwei_android_iot.utils.IotUtil;
import vip.idiy.anwei_android_iot.utils.ViewUtil;
import vip.idiy.anwei_android_iot.utils.handle.EspTouchHandle;
import vip.idiy.anwei_android_iot.views.LayoutBasicInfo;

/**
 * 一键配网
 */
public class SmartConfigWaitActivity extends AppCompatActivity {

    private TextView smart_config_wait_status;
    private LayoutBasicInfo basicInfo_wifi_password;
    private EspTouchHandle espTouchHandle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_config_wait);

        // 设置设透明状态栏,导航栏. 状态栏字体修改为黑色
        ViewUtil.setViewStyle(this, true);

        initView();
    }

    /**
     * 初始化窗口
     */
    private void initView() {
        TextView layout_reuse_head_tv_title = findViewById(R.id.layout_reuse_head_tv_title);
        layout_reuse_head_tv_title.setText(R.string.smart_config_wait);
        // 返回
        findViewById(R.id.layout_reuse_head_return_myImageButton).setOnClickListener(v -> finish());

        smart_config_wait_status = findViewById(R.id.smart_config_wait_status);

        LinearLayout smart_config_wait_linear = findViewById(R.id.smart_config_wait_linear);

        LayoutBasicInfo basicInfo_wifi_name = new LayoutBasicInfo(this).init(false, true, false, false, false, false)
                .setLeftText(getString(R.string.wifi_name))
                .setCentreContent(IotUtil.getConnectedWifiSsid(SmartConfigWaitActivity.this))
                .setLineBackground(R.drawable.back_ripple_gray);

        LayoutBasicInfo basicInfo_wifi_bssid = new LayoutBasicInfo(this).init(false, true, false, false, false, false)
                .setLeftText(getString(R.string.wifi_bssid))
                .setCentreContent(IotUtil.getConnectedWifiBssid(SmartConfigWaitActivity.this))
                .setLineBackground(R.drawable.back_ripple_gray);

        basicInfo_wifi_password = new LayoutBasicInfo(this).init(false, false, false, true, false, false)
                .setLeftText(getString(R.string.wifi_password))
                .setLineBackground(R.drawable.back_ripple_gray);

        smart_config_wait_linear.addView(basicInfo_wifi_name);
        smart_config_wait_linear.addView(basicInfo_wifi_bssid);
        smart_config_wait_linear.addView(basicInfo_wifi_password);

        // 开始配网
        findViewById(R.id.smart_config_wait_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smartConfigWait();
            }
        });

        if(IotUtil.is5G(this)){
            smart_config_wait_status.setText(R.string.smart_config_wait);
        }
    }

    /**
     * 开始配网
     */
    private void smartConfigWait() {
        if(IotUtil.is5G(this)){
            smart_config_wait_status.setText(R.string.smart_config_wait);
            return;
        }

        smart_config_wait_status.setText(getString(R.string.starting_smart_config_wait));
        espTouchHandle = new EspTouchHandle.Builder(SmartConfigWaitActivity.this)
                .setSSID(IotUtil.getConnectedWifiSsid(this))
                .setBSSID(IotUtil.getConnectedWifiBssid(this))
                .setPassWord(basicInfo_wifi_password.getCentreEditContent().trim())
                .setDeviceCount(1) // 设置只广播一个设备, 多个设备可以在这更改
                .build();
        espTouchHandle.setOnEsptouchTaskListener((code, message) -> {
            switch (code) {
                case EspTouchHandle.RESULT_CONFIG_SUCCESS:
                    smart_config_wait_status.setText(getString(R.string.result_config_success));
                    break;
                case EspTouchHandle.RESULT_CONFIG_TIMEOUT:
                    smart_config_wait_status.setText(getString(R.string.result_config_timeout));
                    break;
                default:
                    smart_config_wait_status.setText(getString(R.string.result_config_fail));
                    break;
            }
        });
        espTouchHandle.startConfig();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (espTouchHandle!=null){
            espTouchHandle.stopConfig();
        }
    }
}